package com.example.administrator.basedemos.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 创建者     ZCL
 * 创建时间   2018/7/3 16:58
 * 描述	      ${TODO}
 * <p>
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */
public abstract class BaseFragment extends Fragment{

    private Unbinder mUnbinder;

    public abstract int setLayoutId();

    public abstract void initData();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
        View view = inflater.inflate(setLayoutId(), container, false);
        mUnbinder = ButterKnife.bind(this, view);
        initData();
        initListener();
        return view;
    }

    public void initListener() {

    }


    public void showActivity(Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        startActivity(intent);

    }

    /**
     * show to @param(cls)ï¼Œbut can't finish activity
     */
    public void showActivity(Intent i) {
        startActivity(i);
    }

    public void showActivityForResult(Intent intent, int code) {
        startActivityForResult(intent, code);

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser && isVisible()) {
            onVisibleToUser();
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    /**
     * 当前Fragment可见
     */
    public void onVisibleToUser() {
    }

    /**
     *
     */
    public void showActivityForResult(Class<?> cls, int requestCode) {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClass(getActivity(), cls);
        this.startActivityForResult(intent, requestCode);
    }


    /**
     * skip to @param(cls)ï¼Œand call @param(aty's) finish() method
     */
    public void skipActivity(Class<?> cls) {
        showActivity(cls);
        getActivity().finish();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }

}
